package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysProcess;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysProcessBo;
import com.xmtlxx.system.domain.vo.SysProcessVo;
import com.xmtlxx.system.mapper.SysProcessMapper;
import com.xmtlxx.system.service.ISysProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 业务流程Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysProcessServiceImpl implements ISysProcessService {

    private final SysProcessMapper baseMapper;

    /**
     * 查询业务流程
     *
     * @param pid 业务流程主键
     * @return 业务流程
     */
    @Override
    public SysProcessVo queryById(String pid){
        return baseMapper.selectVoById(pid);
    }

    /**
     * 查询业务流程列表
     *
     * @param bo 业务流程
     * @return 业务流程
     */
    @Override
    public TableDataInfo<SysProcessVo> queryPageList(SysProcessBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<SysProcess> lqw = buildQueryWrapper(bo);
        Page<SysProcessVo> result = baseMapper.selectPageSysProcessList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务流程列表
     *
     * @param bo 业务流程
     * @return 业务流程
     */
    @Override
    public List<SysProcessVo> queryList(SysProcessBo bo) {
//        LambdaQueryWrapper<SysProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectSysProcessList(bo);
    }

    private LambdaQueryWrapper<SysProcess> buildQueryWrapper(SysProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysProcess> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBussid()), SysProcess::getBussid, bo.getBussid());
        lqw.eq(StringUtils.isNotBlank(bo.getMid()), SysProcess::getMid, bo.getMid());
        lqw.eq(StringUtils.isNotBlank(bo.getWid()), SysProcess::getWid, bo.getWid());
        lqw.eq(StringUtils.isNotBlank(bo.getNid()), SysProcess::getNid, bo.getNid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysProcess::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysProcess::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), SysProcess::getOperator, bo.getOperator());
        lqw.between(params.get("beginActime") != null && params.get("endActime") != null,
            SysProcess::getActime ,params.get("beginActime"), params.get("endActime"));
        lqw.eq(StringUtils.isNotBlank(bo.getAction()), SysProcess::getAction, bo.getAction());
        lqw.eq(StringUtils.isNotBlank(bo.getCond()), SysProcess::getCond, bo.getCond());
        lqw.orderByAsc(SysProcess::getSeq);
        return lqw;
    }

    /**
     * 新增业务流程
     *
     * @param bo 业务流程
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysProcessBo bo) {
        SysProcess add = BeanUtil.toBean(bo, SysProcess.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setPid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPid(add.getPid());
        }
        return flag;
    }

    /**
     * 修改业务流程
     *
     * @param bo 业务流程
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysProcessBo bo) {
        SysProcess update = BeanUtil.toBean(bo, SysProcess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务流程
     *
     * @param ids 需要删除的业务流程主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 待办事项
     *
     * @param bo
     * @return
     */
    @Override
    public List<SysProcessVo> selectUndealBuss(SysProcessBo bo) {
        return baseMapper.selectUndealBuss(bo);
    }
}
